package org.eclipse.wst.server.preview.openejb.jetty.loader;

import java.util.Properties;

import org.apache.openejb.loader.SystemInstance;

/**JettyHook : This class has just the method public void hook(). 
 * The method hook is responsible to create the Properties object 
 * that will configure the container. A important property put in 
 * it is the property openejb.libs. That property tells where is 
 * the OpenEJB lib dir, and it was set when Jetty starts. The hook 
 * method is responsible to begin the instantiation process of the 
 * OpenEJB container. That is made calling SystemInstance.init( 
 * Properties pro ). After that, the container is configured, but 
 * not running. That's because the rest of the loader process has 
 * to be made under the OpenEJB ClassLoader hierarchy, instead of 
 * Jetty ClassLoader hierarchy. So the rest of the process if made 
 * calling Embedder.init( Properties pro ) passing the name of the 
 * class that will instantiates the OpenEJB http server.
 * 
 * @author Marcelo de Oliveira Dias
 *
 */
public class JettyHook {
	static void hook(){
		// create the loader
        try{
        	Properties p = new Properties();
        	
        	//The url used to find the EJB's deployed in the OpenEJB container.
        	p.setProperty("openejb.ejbd.uri", "http://127.0.0.1:8080/openejb/ejb");
        	
        	String openejbWebapp = System.getProperty("openejb.webapp");
        	
        	//Set the property used to tell where is the lib directory of the OpenEJB dependencies.
        	p.setProperty("openejb.libs", openejbWebapp + "/WEB-INF/lib");
        	
        	p.setProperty("openejb.home", openejbWebapp );
        	p.setProperty("openejb.base", openejbWebapp );
        	p.setProperty("openejb.loader", "system");
        	System.getProperties().putAll( p );       
        	
        	//Instantiates the OpenEJB system. 
        	SystemInstance.init( p );
        	
        	//Loads and runs the OpenEJB container inside the JettyServer.
    		Embedder embedder = new Embedder("org.eclipse.wst.server.preview.openejb.jetty.loader.OpenEJBLoader");
        	embedder.init( p );
        	
        }catch( Exception e ){
        	e.printStackTrace();
        }
	}
}
